package com.yhgarden.bbs.model.exception;

import com.yhgarden.bbs.model.enums.StatusCode;
import org.springframework.util.ObjectUtils;

public class YhbbsException extends Exception {

    private StatusCode status;
    private String clazz;
    private Object[] massageArgs;


    public YhbbsException() {
        super();
    }

    public YhbbsException(Throwable e) {
        super(e);
    }

    /**
     * 报错需要使用这个，将class参数带上
     * @param status
     * @param clazz
     * @param massageArgs
     */
    public YhbbsException(StatusCode status, Class clazz, Object... massageArgs) {
        super( !ObjectUtils.isEmpty( massageArgs ) ? String.format( status.getMsg(), massageArgs ) : status.getMsg() );
        this.status = status;
        this.clazz = clazz.getName();
        this.massageArgs = massageArgs;
    }

    public YhbbsException(StatusCode status, String message, Class clazz) {
        super( message );
        this.clazz = clazz.getName();
        this.status = status;
    }

    public YhbbsException(StatusCode status, String message, Throwable cause, Class clazz) {
        super(message, cause);
        this.status = status;
    }

    public YhbbsException(StatusCode status, Throwable cause, Class clazz) {
        super(cause);
        this.status = status;
    }

    protected YhbbsException(StatusCode status, String message, Throwable cause, Class clazz, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.status = status;
    }

    @Override
    public String toString() {
        String s = getClass().getName();
        String message = getLocalizedMessage();
        return (message != null) ? (s + ": status=>" +(status == null?"haven't status":status)  +" ,message=>" + message) : s;
    }

    public StatusCode getStatus() {
        return this.status;
    }

    public Object[] getMassageArgs() {
        return this.massageArgs;
    }
    public String getClazz() {
        return this.clazz;
    }
    
}
